﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IronCow;
using Milkify.Interfaces;
using Milkify.Data;
using Milkify.Input;

namespace Milkify.Controllers
{
    public class TaskNoteController : Controller
    {
        private IView mView;

        public TaskNoteController(ControllerContext context, Rtm rtm, IViewContainer viewContainer, IViewFactory viewFactory)
            : base(context, rtm, viewContainer)
        {
            mView = viewFactory.Create("TaskNote");
        }

        public override void Execute()
        {
            // Create the view data.
            var parameter = Context.CommandParameter as NavigateToTaskNoteParameter;
            if (parameter == null)
                throw new InvalidOperationException("No parameter was given for navigating to the task note.");

            var viewData = new TaskNoteViewData(parameter.TaskNote);
            viewData.EditTaskNoteCommand = new DecoupledCommand(EditTaskNote);
            viewData.DeleteTaskNoteCommand = new DecoupledCommand(DeleteTaskNote);
            mView.ViewData = viewData;

            // Change the current view.
            ViewContainer.NavigateToView(mView);
        }

        private void EditTaskNote()
        {
            var parameter = (NavigateToTaskNoteParameter)Context.CommandParameter as NavigateToTaskNoteParameter;
            Redirect("TaskNoteEdit", "EditTaskNote", parameter);
        }

        private void DeleteTaskNote()
        {
            var parameter = (NavigateToTaskNoteParameter)Context.CommandParameter as NavigateToTaskNoteParameter;
            parameter.Task.Notes.Remove(parameter.TaskNote);
            ViewContainer.NavigateBack();
        }
    }
}
